package com.zensun.web.controller.system;

import com.zensun.common.core.domain.R;
import com.zensun.common.utils.spring.ServletUtils;
import com.zensun.framework.web.service.SysLoginService;
import com.zensun.framework.web.service.SysPermissionService;
import com.zensun.framework.web.service.TokenService;
import com.zensun.system.domain.SysUser;
import com.zensun.system.domain.model.LoginBody;
import com.zensun.system.domain.model.LoginByTelephoneBody;
import com.zensun.system.domain.model.LoginUser;
import com.zensun.system.domain.vo.RouterVo4C;
import com.zensun.system.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 登录验证
 *
 * @author gmk
 */
@Api(value = "登录管理", tags = "登录模块")
@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @ApiOperation("账号密码---登录方法")
    @PostMapping("/login")
    public R login(@RequestBody LoginBody loginBody) {

        // 生成令牌
        Map<String, Object> map = loginService.login(loginBody.getTelephone(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        return R.success(map);
    }

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @ApiOperation("手机号验证码---登录方法")
    @PostMapping("/loginByTelephone")
    public R loginByTelephone(@RequestBody LoginByTelephoneBody loginBody) {
        // 生成令牌
        Map<String, Object> map = loginService.loginByTelephone(loginBody.getTelephone(), loginBody.getCode(),
                loginBody.getUuid());
        return R.success(map);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @ApiOperation("获取用户信息")
    @GetMapping("getInfo")
    public R getInfo() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        Map<String, Object> map = new HashMap<>();
        map.put("user", user);
        map.put("roles", roles);
        map.put("permissions", permissions);
        return R.success(map);
    }

    /**
     * 获取路由信息-目录
     *
     * @return 路由信息
     */
    @ApiOperation("获取路由信息-目录")
    @GetMapping("getRouters4M")
    public R<List<RouterVo4C>> getRouters4M() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        return R.success(menuService.selectRouterVo4MByUserId(loginUser.getUser().getUserId()));
    }
}
